## Figure replication code for "Repelling Rape" 
## Replication files for all figures except Figures A.1, A.3, and A.5 (separate replication files using Stata available).
## Updated August 2024

# Load necessary packages.
library(ggplot2)
library(lattice)
library(latticeExtra)

# Set working directory to current folder, which contains all necessary data files.
setwd("....../CrimeFDI_ReplicationFiles_JOP_Jun2024/Data")

##############################################################
### Figure 1: Liberalization Increased India's FDI Inflows ###
##############################################################

# Load Figure 1 top panel data.
fig1top_data <- read.csv("fig1top_data.csv")

# Set panel parameters for a double-paneled figure.
par(mfrow=c(2,1))

# Plot top panel of Figure 1.
plot(fig1top_data$x~as.integer(fig2top_data$year),xlab="year",ylab="rupees (2011 constant millions)",type="l",lty=1,lwd=2, sub="Completed Greenfield FDI Projects (Source: CapEx)")
abline(v=2006,col="red",lty=1)

# Load Figure 1 bottom panel data.
fig1bot_data <- read.csv("fig1bot_data.csv")

# Plot bottom panel of Figure 1.
plot(fig1bot_data$x~fig1bot_data$year,xlab="year",ylab=" US$ (millions)",type="l",lty=2,lwd=2,sub=" Official Data on Greenfield FDI Inflows (Source: RBI)")
abline(v=2006,col="red",lty=1)


###################################################################
### Figure 2: FDI in India over Time: Treated vs Control States ###
###################################################################

# Load data for Figure 2.
fig2_data <- read.csv("fig2_data.csv")

# Plot Figure 2.
plot(fig2_data[which(fig2_data$assign=="treated"),"x"]~fig2_data[which(fig2_data$assign=="treated"),"year"],xlab="year",ylab="rupees (millions)",col="black",type="l",lty=1,lwd=2,ylim=c(0,200000))
lines(fig2_data[which(fig2_data$assign=="control"),"x"]~fig2_data[which(fig2_data$assign=="control"),"year"],xlab="year",ylab="rupees (millions)",col="black",type="l",lty=2,lwd=2,ylim=c(0,200000))
leg.txt=c("treatment","control")
legend("topleft",lty=c(1,2),col=c("black","black"),lwd=c(2,2),leg.txt,bty="n")


############################################################
### Appendix Figure A.2: FDI in India Over Time By Route ###
############################################################

# Reset panel parameters for the figure.
par(mfrow=c(1,1))

# Load data for Figure 2.
figa2_data <- read.csv("figa2_data.csv")

# Create a vector of variables for use in the figure.
vars.use <- c("a. Government (SIA/FIPB)","b. RBI","d. Acquisition of shares *","II. Reinvested earnings +" ,"c. NRI")

# Plot Appendix Figure A2.
plot(figa2_data[which(figa2_data$type==vars.use[2]),"value"]~figa2_data[which(figa2_data$type==vars.use[2]),"variable"],type="l",col="black",xlab="RBI fiscal year",ylab="millions US$",lty=1,lwd=4)
lines(figa2_data[which(figa2_data$type==vars.use[1]),"value"]~figa2_data[which(figa2_data$type==vars.use[1]),"variable"],type="l",col="black",xlab="RBI fiscal year",ylab="millions US$",lty=2,lwd=2)
lines(figa2_data[which(figa2_data$type==vars.use[3]),"value"]~figa2_data[which(figa2_data$type==vars.use[3]),"variable"],type="l",col="black",xlab="RBI fiscal year",ylab="millions US$",lty=3,lwd=2)
lines(figa2_data[which(figa2_data$type==vars.use[4]),"value"]~figa2_data[which(figa2_data$type==vars.use[4]),"variable"],type="l",col="black",xlab="RBI fiscal year",ylab="millions US$",lty=4,lwd=2)
leg.tfiga2_datat=c("automatic route","government route","acqusition","reinvested earnings")
legend("topleft",lty=c(1,2,3,4),col=c("black","black","black","black"),lwd=c(4,2,2,2,2),leg.tfiga2_datat,bty="n")


############################################################################
### Appendix Figure A.4: Year-by-Year Estimates of FDI's Effects on Rape ###
############################################################################

figa4_data <- read.csv("figa4_data.csv")
figa4_data <- figa4_data[which(figa4_data$version=="3_full_controls"),]

ggplot(figa4_data, aes(x=year, y=coef)) + 
  geom_vline(xintercept = 2006.03, color="red", lty=2) + 
  geom_point(position=position_dodge(width=0.4)) + 
  geom_errorbar(aes(ymin=lb, ymax=ub), width=0, position=position_dodge(width=0.4)) + 
  geom_hline(yintercept = 0, alpha=.3, size=.2) + 
  scale_x_continuous(breaks=seq(2003, 2012, by=1)) +
  xlab("Year") + 
  ylab("Coefficient") + 
  theme_classic() + 
  theme(legend.position="bottom")

